package com.digitalchina.model.controller;

import com.digitalchina.common.data.RtnData;
import com.digitalchina.industry.model.EyeModelIndustry;
import com.digitalchina.industry.service.IndustryService;
import com.digitalchina.model.model.EyeModelComment;
import com.digitalchina.industry.model.EyeModelIndustry;
import com.digitalchina.industry.service.IndustryService;
import com.digitalchina.industry.service.SceneService;
import com.digitalchina.model.model.EyeModelData;
import com.digitalchina.model.service.ModelCommentService;
import com.digitalchina.model.service.ModelService;
import com.digitalchina.common.pagination.Page;
import com.digitalchina.common.pagination.PaginationUtils;
import javafx.scene.Scene;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang_ on 2017/7/11.
 * 模型管理
 */
@Controller
@RequestMapping("/modelController")
public class ModelController {
    @Autowired
    private ModelService modelService;
    @Autowired
    private ModelCommentService modelCommentService;
    @Autowired
    private IndustryService industryService;
    @Autowired
    private SceneService sceneService;


    @RequestMapping(value = "/list.html")
    public String modelList(ModelMap map) {
        Map params = new HashMap();
        params.put("industryName", null);
        params.put("startIndex", 0);
        params.put("pageS", 10000);
        params.put("sceneName", null);
        List<Map<String, Object>> industys = industryService.getAllIndustrylList(params);
        List<Map<String, Object>> scenes = sceneService.getAllIndustrylList(params);
        map.put("industys", industys);
        map.put("scenes", scenes);
        return "model/model_list";
    }

    //    @RequestMapping(value = "/model_comment.html")
//    public String commentModel(){
//        return "model/model_comment";
//    }
    @RequestMapping(value = "/list.do")
    public String modelList(@RequestParam(required = false, defaultValue = "2") long pageSize,
                            @RequestParam(required = false, defaultValue = "1") long pageNum,
                            @RequestParam(required = false) String modelName, String status, String hangye, String Cj, ModelMap map, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("modelName", modelName);
        params.put("status", status);
        params.put("hangye", hangye);
        params.put("Cj", Cj);
        int totalSize = modelService.findTotalCount(params);
        params.put("startIndex", (pageNum - 1) * pageSize);
        params.put("pageS", pageSize);
        List<EyeModelData> models = modelService.findAll(params);
        List<EyeModelData> modelsRes = new ArrayList<>();
        try {
            for (EyeModelData eyeModelData : models) {
                eyeModelData.setModelIndustry(modelService.getEyeModelIndustry(eyeModelData.getId().toString()));
                eyeModelData.setModelScene(modelService.getEyeModelScene(eyeModelData.getId().toString()));
                modelsRes.add(eyeModelData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        map.put("models", models);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
//        map.put("totelPage", totalSize % pageSize > 0 ? totalSize / pageSize + 1 : totalSize / pageSize);
        map.put("totalSize", totalSize);
        map.put("currentPage", pageNum);
        return "model/modelTem :: model_list";
    }

    @RequestMapping(value = "/edite_model.html")
    public String editeModel(@RequestParam(required = true) String modelId, ModelMap map, HttpServletRequest request) {
        EyeModelData model = modelService.findById(modelId);
        Map params = new HashMap();
        params.put("industryName", null);
        params.put("startIndex", 0);
        params.put("pageS", 10000);
        params.put("sceneName", null);
        List<Map<String, Object>> industys = industryService.getAllIndustrylList(params);
        List<Map<String, Object>> scenes = sceneService.getAllIndustrylList(params);
        map.put("model", model);
        map.put("industys", industys);
        map.put("scenes", scenes);
        return "model/edite_model";
    }

    /**
     * 更新用户信息
     *
     * @return
     */
    @RequestMapping(value = "/update.do", method = RequestMethod.POST)
    @ResponseBody
    public RtnData updateModelInfo(EyeModelData model) {
        int count = modelService.updata(model);
        if (count > 0) {
            return RtnData.ok("修改成功");
        } else {
            return RtnData.fail("修改失败");
        }
    }

    @RequestMapping(value = "/info_model.html")
    public String infoModel(@RequestParam(required = true) String modelId, ModelMap map, HttpServletRequest request) {
        try {
            EyeModelData model = modelService.findById(modelId);
            map.put("model", model);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "model/info_model";
    }

    @RequestMapping(value = "/model_comment.html")
    public String commentModel(@RequestParam(required = false, defaultValue = "2") long pageSize,
                               @RequestParam(required = false, defaultValue = "1") long pageNum, String startDate, String endDate, String status, String modelId, ModelMap map, HttpServletRequest request) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("modelId", Integer.valueOf(modelId));
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("status", status);
        params.put("startIndex", (pageNum - 1) * pageSize);
        params.put("pageS", pageSize);
        int totalSize = 0;
        List modelComment = null;
        try {
            totalSize = modelCommentService.findTotalCount(params);
            modelComment = modelCommentService.findAll(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("modelComment", modelComment);
        map.put("modelId", modelId);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
//        map.put("totelPage", totalSize % pageSize > 0 ? totalSize / pageSize + 1 : totalSize / pageSize);
        map.put("totalSize", totalSize);
        map.put("currentPage", pageNum);
        return "model/model_comment";
    }

    @RequestMapping(value = "/upAdown.do")
    @ResponseBody
    public RtnData upAdown(String status, String modelId,String noPassMes) {
        if ("10002".equals(status)) {
            status = "10005";
        } else if ("10003".equals(status)) {
            status = "10002";
        }
        EyeModelData eyeModelData = new EyeModelData();
        eyeModelData.setModelState(status);
        if (!StringUtils.isEmpty(noPassMes)){
            eyeModelData.setNoPassMsg(noPassMes);
        }
        eyeModelData.setId(Integer.valueOf(modelId));
        try {
            if (modelService.updata(eyeModelData) > 0) {
                return RtnData.ok("");
            } else {
                return RtnData.fail("");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RtnData.ok("");
        }

    }

    @RequestMapping(value = "/updataComment.do")
    @ResponseBody
    public RtnData updataComment(String reComment, String id, String status) {
        EyeModelComment eyeModelComment = new EyeModelComment();
        eyeModelComment.setId(Integer.valueOf(id));
        if (!StringUtils.isEmpty(reComment)) {
            eyeModelComment.setReplyContent(reComment);
            Short st = 2;
            eyeModelComment.setStatus(st);
        }
        if (!StringUtils.isEmpty(status)) {
            eyeModelComment.setStatus(Short.valueOf(status));
        }

        if (modelCommentService.update(eyeModelComment) > 0) {
            return RtnData.ok("");
        } else {
            return RtnData.fail("");
        }
    }

    @RequestMapping(value = "/modelExamineInfo.do")
    @ResponseBody
    public RtnData modelExamineInfo(String modelId) {

        Map<String,String> modelExam=modelService.getModelExamineInfo(modelId);
        return RtnData.ok(modelExam);

    }
}
